package com.lysj.bhp.api.controller;

import com.lysj.bhp.investment.domain.Reward;
import com.lysj.bhp.user.domain.User;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import com.lysj.bhp.wallet.domain.BhpcWallet;
import com.lysj.bhp.wallet.domain.BitCoinWallet;
import com.lysj.bhp.wallet.domain.EthereumWallet;
import com.lysj.bhp.withdraw.base.WithdrawContent;
import com.lysj.bhp.withdraw.domain.Poundage;
import com.lysj.bhp.withdraw.domain.WithDraw;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;

@RestController
@RequestMapping("/api/v2/user")
@Api(value = "ApiWithdrawApplyController", tags = "提币申请接口")
public class ApiWithdrawApplyController extends WithdrawContent {

    @ApiOperation(value = "提币申请", notes = "需要提币地址,提币数量,交易密码,钱包类型,type 10:比特币;20:以太坊;100:BHPC;110:BHPC奖励金")
    @PostMapping(value = "/add_withdraw_apply")
    public Resp withdraw(String address, BigDecimal amount, String tradePassword, int type) {

        if (ParamUtil.isBlack(address)) {
            return new Resp(Resp.PARAM_ERROR, "请输入提币地址");
        }
        if (BigDecimal.ZERO.compareTo(amount) == 1) {  //提币数量小于0
            return new Resp(Resp.PARAM_ERROR, "提币数量错误");
        }
        if (ParamUtil.isBlack(tradePassword)) {
            return new Resp(Resp.PARAM_ERROR, "交易密码不能为空");
        }
        if (!ParamUtil.md5(tradePassword).equals(currentUser().getTradePassword())) {
            return new Resp(Resp.PARAM_ERROR, "交易密码有误");
        }

        WithDraw withDraw = new WithDraw();
        //获取提币手续费
        Poundage poundage = poundageRepository.findByType(type);
        BigDecimal transferFee = new BigDecimal(BigInteger.ZERO);// 提币手续费默认为0
        // 平台设置手续费
        if (!ParamUtil.isBlack(poundage)) {
            transferFee = poundage.getTransferFee();
            if ((amount.compareTo(poundage.getMiniNumber()) == -1)) { //手续费
                return new Resp(Resp.PARAM_ERROR, "提币数量最少为", poundage.getMiniNumber().setScale(6,BigDecimal.ROUND_DOWN));
            }
        }
//        amount = amount.subtract(transferFee);
        switch (type) {  //10:比特币;20:以太坊;100:BHPC;110:BHPC_Bouns
            case 100:
                BhpcWallet bhpcWallet = bhpcWalletRepository.findByUserId(currentUser().getUserId());

                if(null==bhpcWallet){
                    return new Resp(Resp.PARAM_ERROR, "您的BHPC数量不足");
                }

                if ((bhpcWallet.getAvailBalance().subtract(amount)).compareTo(new BigDecimal(BigInteger.ZERO)) == -1) { //钱包的可用数额跟提币数量进行比较
                    return new Resp(Resp.PARAM_ERROR, "您最大提币数额为",bhpcWallet.getAvailBalance().setScale(6,BigDecimal.ROUND_DOWN));
                }
                withDraw.setUserId(currentUser().getUserId());
                withDraw.setEmail(currentUser().getEmail());
                withDraw.setAddress(address);
                withDraw.setType(100);
                withDraw.setAmount(amount);
                withDraw.setFeeAmount(transferFee);
                withDraw.setCreateDate(new Date());
                withDraw.setStatus(0);
                withDraw.setCheckStatus(3);
                withDraw.setName("BHPC");
                withDraw.setChineseName("算力现金");
                //获取该用户的BHPC钱包,生成冻结余额,修改可用余额
                bhpcWallet.setAvailBalance((bhpcWallet.getAvailBalance().subtract(amount)));
                bhpcWallet.setFreezeBalance(bhpcWallet.getFreezeBalance().add(amount));
                bhpcWalletRepository.save(bhpcWallet);
                withdrawRepository.save(withDraw);
                break;
            case 110:
                poundage = poundageRepository.findByType(100);
                if (!ParamUtil.isBlack(poundage)) {
                    transferFee = poundage.getTransferFee();
                    if ((amount.compareTo(poundage.getMiniNumber()) == -1)) { //手续费
                        return new Resp(Resp.PARAM_ERROR, "提币数量最少为",poundage.getMiniNumber().setScale(6,BigDecimal.ROUND_DOWN));
                    }
                }
//                amount = amount.subtract(transferFee);
                Reward rewardWallet = rewardRepository.findByUserIdAndType(currentUser().getUserId(), 100);

                if(null==rewardWallet){
                    return new Resp(Resp.PARAM_ERROR, "您的BHPC奖励数量不足");
                }

                if ((rewardWallet.getAvailBalance().subtract(amount)).compareTo(new BigDecimal(BigInteger.ZERO)) == -1) {
                    return new Resp(Resp.PARAM_ERROR, "您的BHPC奖励最大提币数额为",rewardWallet.getAvailBalance().setScale(6,BigDecimal.ROUND_DOWN));
                }
                withDraw.setUserId(currentUser().getUserId());
                withDraw.setEmail(currentUser().getEmail());
                withDraw.setAddress(address);
                withDraw.setType(110);
                withDraw.setAmount(amount);
                withDraw.setFeeAmount(transferFee);
                withDraw.setCreateDate(new Date());
                withDraw.setStatus(0);
                withDraw.setCheckStatus(3);
                withDraw.setName("BHPC");
                withDraw.setChineseName("算力现金");
                //获取该用户的BHPC奖励钱包,生成冻结余额,修改可用余额
                rewardWallet.setAvailBalance(rewardWallet.getAvailBalance().subtract(amount));
                rewardWallet.setFreezeBalance(rewardWallet.getFreezeBalance().add(amount));
                rewardRepository.save(rewardWallet);
                withdrawRepository.save(withDraw);
                break;
            case 10:
                BitCoinWallet bitCoinWallet = bitcoinWalletRepository.findByUserId(currentUser().getUserId());

                if(null==bitCoinWallet){
                    return new Resp(Resp.PARAM_ERROR, "您的BTC数量不足");
                }

                if ((bitCoinWallet.getAvailBalance().subtract(amount)).compareTo(new BigDecimal(BigInteger.ZERO)) == -1) { //手续费
                    return new Resp(Resp.PARAM_ERROR, "您的BTC最大提币数量为",bitCoinWallet.getAvailBalance().setScale(6,BigDecimal.ROUND_DOWN));
                }

                withDraw.setUserId(currentUser().getUserId());
                withDraw.setEmail(currentUser().getEmail());
                withDraw.setAddress(address);
                withDraw.setType(10);
                withDraw.setAmount(amount);
                withDraw.setFeeAmount(transferFee); //需要修改
                withDraw.setCreateDate(new Date());
                withDraw.setStatus(0);
                withDraw.setCheckStatus(3);
                withDraw.setName("BTC");
                withDraw.setChineseName("比特币");
                //获取该用户的BHPC钱包,生成冻结余额,修改可用余额
                bitCoinWallet.setAvailBalance(bitCoinWallet.getAvailBalance().subtract(amount));
                bitCoinWallet.setFreezeBalance(bitCoinWallet.getFreezeBalance().add(amount));
                bitcoinWalletRepository.save(bitCoinWallet);
                withdrawRepository.save(withDraw);
                break;
            case 20:
                EthereumWallet ethereumWallet = ethereumWalletRepository.findByUserId(currentUser().getUserId());

                if(null==ethereumWallet){
                    return new Resp(Resp.PARAM_ERROR, "您的ETH数量不足");
                }

                if ((ethereumWallet.getAvailBalance().subtract(amount)).compareTo(new BigDecimal(BigInteger.ZERO)) == -1) { //手续费
                    return new Resp(Resp.PARAM_ERROR, "您的ETH最大提币数量为",ethereumWallet.getAvailBalance().setScale(6,BigDecimal.ROUND_DOWN));
                }
                withDraw.setUserId(currentUser().getUserId());
                withDraw.setEmail(currentUser().getEmail());
                withDraw.setAddress(address);
                withDraw.setType(20);
                withDraw.setAmount(amount);
                withDraw.setFeeAmount(transferFee);
                withDraw.setCreateDate(new Date());
                withDraw.setStatus(0);
                withDraw.setCheckStatus(3);
                withDraw.setName("ETH");
                withDraw.setChineseName("以太坊");
                //获取该用户的BHPC钱包,生成冻结余额,修改可用余额
                ethereumWallet.setAvailBalance((ethereumWallet.getAvailBalance().subtract(amount)));
                ethereumWallet.setFreezeBalance(ethereumWallet.getFreezeBalance().add(amount));
                ethereumWalletRepository.save(ethereumWallet);
                withdrawRepository.save(withDraw);
                break;
            default:
                return new Resp(Resp.PARAM_ERROR, "提币类型错误");
        }
        return new Resp("提币申请成功");
    }
}
